import javax.crypto.interfaces.PBEKey;
import java.util.Stack;

public class Sort {
    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    // 插入排序
    public static void insertSort(int[] array){
        for (int i = 1; i < array.length; i++) {
            int temp = array[i];
            int j = i-1; //标记要插入数的前一个数
            for(; j >= 0; j--) {
                if(array[j] > temp) {    //前一个数比当前要插入的数要大
                    array[j+1] = array[j];
                }
                else {
                    break;
                }
            }
            array[j+1] = temp;
        }
    }


    // 希尔排序
    public static void shellSort(int[] array){
        int gap = array.length;
        while(gap > 1) {
            gap/=2;
            shell(array,gap);
        }
    }
    private static void shell(int[] array, int gap) {
        for (int i = 1; i < array.length; i++) {
            int temp = array[i];
            int j = i-gap; //标记要插入数的前一个数
            for(; j >= 0; j-=gap) {
                if(array[j] > temp) {    //前一个数比当前要插入的数要大
                    array[j+gap] = array[j];
                }
                else {
                    break;
                }
            }
            array[j+gap] = temp;
        }
    }

    // 选择排序
    public static void selectSort(int[] array){
        for(int j = 0; j < array.length-1; j++) {
            int minIndex = j;
            for(int i = j+1; i < array.length; i++) {
                if(array[i] < array[minIndex]) {
                    minIndex = i;
                }
            }
            swap(array,minIndex,j);
        }

    }


    // 堆排序
    public static void heapSort(int[] array){
        createHeap(array);
        int end = array.length-1;
        while(end > 0) {
            swap(array,0,end--);
            shiftDown(array,0,end);
        }


    }

    private static void createHeap(int[] array) {
        for(int i = (array.length-1-1)/2; i >= 0; i--) {
            shiftDown(array,i,array.length);
        }
    }

    private static void shiftDown(int[] array, int parent,int end) {
        int child = parent*2 +1;
        while(child < end) {
            child = child+1<end && array[child+1] > array[child] ? child +1 : child;
            if(array[child] > array[parent]) {
                swap(array,child,parent);
                parent = child;
                child = parent*2 +1;
            }
            else {
                break;
            }
        }
    }


    // 冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length - 1; i++) {
            boolean flg = true;
            for(int j = 0; j < array.length-1-i; ++j) {
                if(array[j] > array[j+1]) { //前一个比后一个大就交换
                    swap(array,j,j+1);
                    flg = false;
                }
            }
            if(flg) {
                return;
            }
        }
    }

    // 快速
    public static void quickSort(int[] array){
        quick(array,0,array.length-1);
    }
    public static void quick(int[] array, int left , int right) {
        if(left >= right) {
            return;
        }

        int midIndex = getMidIndex(array,left,right);
        swap(array,left,midIndex);

        int mid = partition2(array, left , right);
        quick(array,left,mid-1);
        quick(array,mid+1,right);
    }

    //三数取中
    public static int getMidIndex(int[] arr,int left, int right) {
        int mid = left + right >> 1;

        if(arr[left] < arr[right]) {
            if(arr[mid] < arr[left]) {
                return left;
            }
            else if(arr[mid] > arr[right]) {
                return right;
            }
            else {
                return mid;
            }
        }
        else{
            if(arr[mid] > arr[left]) {
                return left;
            }
            else if(arr[mid] < arr[right]) {
                return right;
            }
            else {
                return mid;
            }
        }
    }


    //（Hoare版本）
    public static int partition(int[] arr, int left , int right) {
        int key = arr[left];
        int keyi = left;

        while(left < right) {
            while (left < right && arr[right] >= key) { //找到小于key的位置停下
                right--;
            }
            while (left < right && arr[left] <= key) {  //找到大于key的位置停下
                left++;
            }
            swap(arr,left,right);
        }
        swap(arr,left,keyi);
        return left;
    }

    //挖坑法
    public static int partition2(int[] arr, int left , int right) {
        int key = arr[left];
        while(left < right) {
            while (left < right && arr[right] >= key) { //找到小于key的位置停下
                right--;
            }
            arr[left] = arr[right];
            while (left < right && arr[left] <= key) {  //找到大于key的位置停下
                left++;
            }
           arr[right] = arr[left];
        }
        arr[left] = key;
        return left;
    }

    //前后指针法
    public static int partition3(int[] arr, int left , int right) {
        int key = arr[left];
        int prev = left;
        int cur = left+1;

        while(cur <= right) {
            if(arr[cur] < key && arr[++prev] != arr[cur]) {
                swap(arr,prev,cur);
            }
            cur++;
        }
        swap(arr,prev,left);
        return prev;
    }

    //快速排序非递归
    public static void quickSortNor(int[] array) {
        Stack<Integer> st = new Stack<>();
        int left = 0;
        int right = array.length-1;
        int midIndex = getMidIndex(array,left,right);
        swap(array,left,midIndex);
        int pivot = partition2(array,left,right);
        if(pivot-1 > left){
            st.push(left);
            st.push(pivot-1);
        }
        if(pivot+1<right) {
            st.push(pivot+1);
            st.push(right);
        }
        while(!st.isEmpty()) {
            right = st.pop();
            left = st.pop();
            midIndex = getMidIndex(array,left,right);
            swap(array,left,midIndex);
            pivot = partition2(array,left,right);
            if(pivot-1 > left){
                st.push(left);
                st.push(pivot-1);
            }
            if(pivot+1<right) {
                st.push(pivot+1);
                st.push(right);
            }
        }
    }


    //归并排序
    public static void mergeSort(int[] array) {
        mergeSortFunc(array,0,array.length-1);
    }

    private static void  mergeSortFunc(int[] array,int left, int right) {
        if(left >= right) {
            return;
        }

        int mid =( left + right ) / 2;
        mergeSortFunc(array,left,mid);
        mergeSortFunc(array,mid+1,right);
        merge(array,left,right,mid);
    }

    public static void merge(int[] arr, int left,int right,int mid) {
        int[] help = new int[right-left+1];  //创建辅助数组
        int k = 0;
        int s1 = left;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = right;
        while(s1 <= e1 && s2 <= e2) {
            if(arr[s1] <= arr[s2]) {
                help[k++] = arr[s1++];
            }
            else {
                help[k++] = arr[s2++];
            }
        }
        while (s1 <= e1) {
            help[k++] = arr[s1++];
        }
        while (s2 <= e2) {
            help[k++] = arr[s2++];
        }

        //将排好序的数组刷给原数组
        for(int i = left; i<=right; ++i) {
            arr[i] = help[i-left];
        }
    }

    //归并排序非递归
    public static void mergeSortNor(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for(int i = 0; i < array.length; i+=2*gap ){
                int left = i;
                int mid = i + gap - 1;
                int right = i+2*gap-1;
                if(mid >= array.length) {
                    mid = array.length -1 ;
                }
                if(right >= array.length) {
                    right = array.length - 1;
                }
                merge(array,left,right,mid);
            }
            gap*=2;
        }

    }

}
